// import { createStore } from "redux";
import { createStore, applyMiddleware, combineReducers } from './redux';

function count(state = 0, action) {
  switch (action.type) {
    case "ADD":
      return state + 1;
    case "MINUS":
      return state - 1;
    default:
      return state;
  }
}
function msg(state = 'msg', action) {
  switch (action.type) {
    case "SET":
      return state = 'omg';
    default:
      return state;
  }
}
const rootReducer = combineReducers({
  count,
  msg
});
const store = createStore(
  rootReducer,
  applyMiddleware(logger, thunk)
);

function logger({ getState }) {
  // next指的是聚合函数(第一次的时候是dispatch函数)，聚合函数主要看compose(相当于compose的那个a)
  // action：dispatch的参数，有可能是函数、异步函数
  return next => action => {
    console.log("*******************************"); //sy-log

    console.log(action.type + "执行了！"); //sy-log

    let prevState = getState();
    console.log("prev state", prevState); //sy-log

    const returnValue = next(action);
    let nextState = getState();
    console.log("next state", nextState); //sy-log

    console.log("*******************************"); //sy-log
    return returnValue;
  };
}
function thunk({ dispatch, getState }) {
  // next指的是聚合函数(第一次的时候是dispatch函数)，聚合函数主要看compose(相当于compose的那个a)
  // action：dispatch的参数，有可能是函数、异步函数
  return next => action => {
    if (typeof action === "function") {
      // 参数：ReduxPage中使用的参数
      return action(dispatch, getState);
    }
    return next(action);
  };
}
export default store;